package app.models.brand;

import com.github.sog.annotation.TableBind;
import com.github.sog.config.StringPool;
import com.github.sog.controller.datatables.core.ColumnDef;
import com.github.sog.controller.datatables.core.DatatablesCriterias;
import com.github.sog.libs.AppFunc;
import com.github.sog.plugin.sqlinxml.SqlKit;
import com.google.common.base.Strings;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import org.joda.time.DateTime;

import java.util.List;

/**
 * <p>
 * .
 * </p>
 *
 * @author liuhui
 * @version 1.0 2014-06-05 14:21
 * @since JDK 1.6
 */
@TableBind(tableName = "ol_brand_daily_stat")
public class DailyStat extends Model<DailyStat> {
    public static final DailyStat dao = new DailyStat();


    private static final long serialVersionUID = -7250877884133710406L;


    public Page<DailyStat> datatable(DatatablesCriterias criterias, String start_date, String end_date) {
        StringBuilder sb = new StringBuilder(" WHERE 1=1 ");
        if (!Strings.isNullOrEmpty(start_date)) {
            sb.append(" AND count_date >=str_to_date('").append(start_date).append("','%Y-%m-%d')");
        }

        if (!Strings.isNullOrEmpty(end_date)) {
            sb.append(" AND count_date <=str_to_date('").append(end_date).append("','%Y-%m-%d')");
            ;
        }

        int pageSize = criterias.getDisplaySize();
        int start = criterias.getDisplayStart() / pageSize + 1;
        final List<ColumnDef> sortingColumnDefs = criterias.getSortingColumnDefs();

        String where = SqlKit.sql("dailystat.columns-where") + sb.toString();

        if (sortingColumnDefs != null && !sortingColumnDefs.isEmpty()) {
            StringBuilder orderBy = new StringBuilder();
            for (ColumnDef sortingColumnDef : sortingColumnDefs)
                if (sortingColumnDef.isSortable()) {
                    orderBy.append(sortingColumnDef.getName()).append(StringPool.SPACE).append(sortingColumnDef.getSortDirection().toString());
                }
            final String byColumns = orderBy.toString();
            if (!Strings.isNullOrEmpty(byColumns)) {
                where += " ORDER BY " + byColumns;
            }
        }
        if (!where.contains("ORDER")) {
            where += " ORDER BY count_date DESC";
        }

        return paginate(start, pageSize, SqlKit.sql("dailystat.columns-select"), where);
    }

    public List<DailyStat> findByBeginAndEndDate(String start_date, String end_date) {
        String sql = SqlKit.sql("dailystat.columns-select") + SqlKit.sql("dailystat.columns-where");
        if (!Strings.isNullOrEmpty(start_date) && !Strings.isNullOrEmpty(end_date)) {
            sql = sql + SqlKit.sql("dailystat.columns-where-export");
            return find(sql, start_date, end_date);
        } else {
            return find(sql);
        }
    }

    public List<DailyStat> getDaiyTop() {
        return find(SqlKit.sql("dailystat.getDaiyTop"), DateTime.now().toString(AppFunc.DATE_TIME_PATTERN_YYYY_MM_DD));
    }

    public DailyStat findByBrandAndDay(long brand_id, String day) {
        return findFirst(SqlKit.sql("dailystat.findByBrandAndDay"), brand_id, day);
    }
}
